package com.newcoin.admin.util;

import com.newcoin.admin.entity.XgbWithdraw;
import org.apache.poi.xssf.usermodel.*;

import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.util.List;

public class ExcelUtil {


    public static void exportExcel(HttpServletResponse response, List<XgbWithdraw> list) throws Exception {
        String[] header = {"提现单号", "手机号", "金额(元)", "实际打款", "银行卡绑定姓名", "银行卡号", "银行卡开户信息", "时间", "状态"};
        XSSFWorkbook xssfWorkbook = new XSSFWorkbook();
        XSSFSheet xssfSheet = xssfWorkbook.createSheet();
        XSSFRow row = xssfSheet.createRow(0);
        for (int i = 0; i < header.length; i++) {
            XSSFCell cell = row.createCell(i);
            XSSFRichTextString xssfRichTextString = new XSSFRichTextString(header[i]);
            cell.setCellValue(xssfRichTextString);
        }
        for (int i = 0; i < list.size(); i++) {
            XSSFRow row1 = xssfSheet.createRow(i + 1);
            for (int j = 0; j < header.length; j++) {
                XSSFCell cell = row1.createCell(j);
                XSSFRichTextString xssfRichTextString = null;
                switch (j) {
                    case 0:
                        xssfRichTextString = new XSSFRichTextString(list.get(i).getId());
                        break;
                    case 1:
                        xssfRichTextString = new XSSFRichTextString(list.get(i).getMobile());
                        break;
                    case 2:
                        xssfRichTextString = new XSSFRichTextString(String.valueOf(list.get(i).getAmount()));
                        break;
                    case 3:
                        xssfRichTextString = new XSSFRichTextString(String.valueOf(list.get(i).getAddress()));
                        break;
                    case 4:
                        xssfRichTextString = new XSSFRichTextString(list.get(i).getCreateDate());
                        break;
                    case 5:
                        if (list.get(i).getState() == 0) {
                            xssfRichTextString = new XSSFRichTextString("待处理");
                        } else if (list.get(i).getState() == 1) {
                            xssfRichTextString = new XSSFRichTextString("已处理");
                        } else if (list.get(i).getState() == 2) {
                            xssfRichTextString = new XSSFRichTextString("已驳回");
                        }
                        break;
                }
                cell.setCellValue(xssfRichTextString);
            }
        }
        response.setContentType("application/octet-stream");
        response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode("提现记录.xlsx", "UTF-8"));
        response.flushBuffer();
        xssfWorkbook.write(response.getOutputStream());
        xssfWorkbook.close();
    }
}
